package com.qingmi.farmer.ui.buy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qingmi.farmer.R;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.model.GoodsSort;
import com.qingmi.farmer.data.model.GoodsType;
import com.qingmi.farmer.data.model.Supplier;
import com.wjjun.customview.views.CheckedLinearLayout;
import com.wjjun.customview.views.CheckedTextView;
import com.wjjun.customview.views.dropdownmenu.CheckTextAdapter;
import com.wjjun.customview.views.dropdownmenu.DoubleListView;
import com.wjjun.customview.views.dropdownmenu.MenuAdapter;
import com.wjjun.customview.views.dropdownmenu.OnFilterItemClickListener;
import com.wjjun.customview.views.dropdownmenu.SimpleTextAdapter;
import com.wjjun.customview.views.dropdownmenu.SingleListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 筛选对话框
 * Created by 67535 on 2018/5/21.
 */
public class GoodsMenuAdapter extends MenuAdapter {

    private Context context;
    private LayoutInflater inflater;

    private List<String> menuTabs = new ArrayList<>();

    private DoubleListView<GoodsType, GoodsType> typeListView;
    private List<GoodsType> typeList = new ArrayList<>();
    private int typeIndex = 0;
    private int varietyIndex = 0;

    private SingleListView<Supplier> supplierListView;
    private List<Supplier> supplierList = new ArrayList<>();
    private int supplierIndex = 0;

    private SingleListView<GoodsSort> sortListView;
    private List<GoodsSort> sortList = new ArrayList<>();
    private int sortIndex = 0;


    public GoodsMenuAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        init();
    }

    private void init() {
        menuTabs.add(context.getString(R.string.txt_all));
        menuTabs.add(context.getString(R.string.txt_type));
        menuTabs.add(context.getString(R.string.txt_merchant));
        menuTabs.add(context.getString(R.string.txt_sort));

        initData();
    }

    private void initData() {
        sortList.add(new GoodsSort(DataSetting.GOODS_SORT_DEFAULT, context.getString(R.string.txt_def_sort)));
        sortList.add(new GoodsSort(DataSetting.GOODS_SORT_PRICE_ASC, context.getString(R.string.txt_sort_price)));
        sortList.add(new GoodsSort(DataSetting.GOODS_SORT_PRICE_DESC, context.getString(R.string.txt_sort_price_desc)));
        sortList.add(new GoodsSort(DataSetting.GOODS_SORT_SELL_ASC, context.getString(R.string.txt_sort_sell_count)));
        sortList.add(new GoodsSort(DataSetting.GOODS_SORT_SELL_DESC, context.getString(R.string.txt_sort_sell_count_desc)));
        setTypeList(null);
        setSupplierList(null);
    }


    public void setTypeList(List<GoodsType> typeList) {
        if (typeList == null) {
            typeList = new ArrayList<>();
        }
        GoodsType all = new GoodsType("0", context.getString(R.string.txt_all_type));
        List<GoodsType> list = new ArrayList<>();
        list.add(new GoodsType("0", context.getString(R.string.txt_all_type)));
        all.setChildParameter(list);
        typeList.add(0, all);
        this.typeList = typeList;
    }

    public void setSupplierList(List<Supplier> supplierList) {
        if (supplierList == null) {
            supplierList = new ArrayList<>();
        }
        supplierList.add(0, new Supplier("0", context.getString(R.string.txt_all_merchant)));
        this.supplierList = supplierList;
    }

    public void setDefaultValue() {
        typeIndex = 0;
        varietyIndex = 0;
        supplierIndex = 0;
        sortIndex = 0;

        menuTabs.set(0, context.getString(R.string.txt_all));
        menuTabs.set(1, context.getString(R.string.txt_type));
        menuTabs.set(2, context.getString(R.string.txt_merchant));
        menuTabs.set(3, context.getString(R.string.txt_sort));

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (menuTabs == null) {
            return 0;
        }
        return menuTabs.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            return null;
        } else if (position == 1) {
            initType();
            return typeListView;
        } else if (position == 2) {
            initMerchant();
            return supplierListView;
        } else if (position == 3) {
            initSort();
            return sortListView;
        }
        return null;
    }

    private void initType() {
        if (typeListView == null) {
            typeListView = new DoubleListView<GoodsType, GoodsType>(context)
                    .leftAdapter(new SimpleTextAdapter<GoodsType>(typeList, context) {
                        @Override
                        public String provideText(GoodsType goodsType) {
                            return goodsType.toString();
                        }
                    }).rightAdapter(new CheckTextAdapter<GoodsType>(null, context) {
                        @Override
                        public String provideText(GoodsType Goodstype) {
                            return Goodstype.toString();
                        }
                    }).onLeftItemClickListener(new DoubleListView.OnLeftItemClickListener<GoodsType, GoodsType>() {
                        @Override
                        public List<GoodsType> provideRightList(GoodsType item, int position) {
                            return item.getChildParameter();
                        }
                    }).onRightItemClickListener(new DoubleListView.OnRightItemClickListener<GoodsType, GoodsType>() {
                        @Override
                        public void onRightItemClick(GoodsType item, GoodsType childItem) {
                            typeIndex = typeList.indexOf(item);
                            varietyIndex = typeList.get(typeIndex).getChildParameter().indexOf(childItem);

                            //类型
                            if (typeIndex == 0) {//全部类型
                                menuTabs.set(1, context.getString(R.string.txt_type));
                            } else {
                                menuTabs.set(1, childItem.getName());
                            }
                            if (listener != null) {
                                listener.onFilterDone(1, item.getId(), childItem.getId());
                            }
                        }
                    });
        }
        typeListView.setLeftList(typeList, typeIndex);
        typeListView.setRightList(typeList.get(typeIndex).getChildParameter(), varietyIndex);
    }

    private void initMerchant() {
        if (supplierListView == null) {
            supplierListView = new SingleListView<Supplier>(context)
                    .adapter(new CheckTextAdapter<Supplier>(supplierList, context) {
                        @Override
                        public String provideText(Supplier supplier) {
                            return supplier.toString();
                        }
                    }).onItemClick(new OnFilterItemClickListener<Supplier>() {
                        @Override
                        public void onItemClick(int position, Supplier item) {
                            supplierIndex = position;

                            //商家
                            if (supplierIndex == 0) {//全部商家
                                menuTabs.set(2, context.getString(R.string.txt_merchant));
                            } else {
                                menuTabs.set(2, item.getSupplierName());
                            }

                            if (listener != null) {
                                listener.onFilterDone(2, item.getSupplierId(), "");
                            }
                        }
                    });
        }

        supplierListView.setList(supplierList, supplierIndex);
    }

    private void initSort() {
        if (sortListView == null) {
            sortListView = new SingleListView<GoodsSort>(context)
                    .adapter(new CheckTextAdapter<GoodsSort>(sortList, context) {
                        @Override
                        public String provideText(GoodsSort goodsSort) {
                            return goodsSort.toString();
                        }
                    }).onItemClick(new OnFilterItemClickListener<GoodsSort>() {
                        @Override
                        public void onItemClick(int position, GoodsSort item) {
                            sortIndex = position;

                            //排序
                            if (sortIndex == 0) {   //默认排序
                                menuTabs.set(3, context.getString(R.string.txt_sort));
                            } else {
                                menuTabs.set(3, item.getName());
                            }
                            if (listener != null) {
                                listener.onFilterDone(3, item.getId(), "");
                            }
                        }
                    });
        }
        sortListView.setList(sortList, sortIndex);
    }

    @Override
    public View getMenuItemView(int index, ViewGroup parentContainer) {
        View view = inflater.inflate(R.layout.layout_tab_item, null);
        CheckedTextView checkedTextView = view.findViewById(R.id.tv_tab);

        MenuViewHolder viewHolder = new MenuViewHolder();
        viewHolder.linearLayout = (CheckedLinearLayout) view;
        viewHolder.textView = checkedTextView;
        view.setTag(viewHolder);

        checkedTextView.setText(menuTabs.get(index));

        if (index == 0) {   //全部
            viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_selector, 0);
        }

        return view;
    }

    @Override
    public void notifyItemChange(View view, int index, boolean isCheck, boolean isClose) {
        MenuViewHolder viewHolder = (MenuViewHolder) view.getTag();
        viewHolder.linearLayout.setChecked(isCheck);
        viewHolder.textView.setText(menuTabs.get(index));

        if (index == 0 && isClose) {   //全部
            viewHolder.linearLayout.setChecked(true);
            viewHolder.textView.setChecked(true);
        }
    }

    private class MenuViewHolder {
        CheckedLinearLayout linearLayout;
        CheckedTextView textView;
    }

}
